package com.zhiyi.app.controller;

import com.github.pagehelper.PageInfo;
import com.zhiyi.app.annotation.ZhiYiLog;
import com.zhiyi.app.entity.AppInfo;
import com.zhiyi.app.entity.DevUser;
import com.zhiyi.app.service.AppInfoService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;

import static com.zhiyi.app.common.UpdateAppStatus.SUCCESS_1;
import static com.zhiyi.app.common.UpdateAppStatus.SUCCESS_3;

/**
 * @author shichenyang
 */
@Controller
@RequestMapping("/app")
public class AppInfoController {

    @Autowired
    private AppInfoService appInfoService;

    /**
     *
     * @param appInfo 模糊查询所用的参数
     * @param page 当前页
     * @param limit 显示条数
     * @return
     */
    @ZhiYiLog("查询显示app信息")
    @RequestMapping("/list")
    public String list(AppInfo appInfo, Integer page,
                       Integer limit, Model model, HttpSession session){
        DevUser u= (DevUser) session.getAttribute("user");
        appInfo.setDevid(u.getId());
        PageInfo info=appInfoService.list(appInfo,page==null?1:page,limit==null?5:limit);
        model.addAttribute("info",info);
       return "devindex";
    }

    @RequestMapping("/export")
    public void export(AppInfo appInfo, HttpServletResponse response,HttpSession session){
        DevUser u= (DevUser) session.getAttribute("user");
        appInfo.setDevid(u.getId());
        HSSFWorkbook workbook=appInfoService.excel(appInfo);
        OutputStream out= null;
        try {
            response.setHeader("Content-Disposition","attachment; filename=app信息.xls");

            out= response.getOutputStream();
            workbook.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    @PostMapping("/add")
    public String add( AppInfo appInfo, HttpSession session){
        //当前用户
        DevUser u= (DevUser) session.getAttribute("user");
//        设置创建人
        appInfo.setCreatedby(u.getId());
//        设置修改人
        appInfo.setModifyby(u.getId());
//        设置开发者ID
        appInfo.setDevid(u.getId());
        if (appInfoService.add(appInfo)){
            return "redirect:/app/list";
        }
        return "appinfoadd";
    }
    @GetMapping("/before/{id}")
    public String updateBefore(@PathVariable Integer id,Model model){
        AppInfo appInfo=appInfoService.updateBefore(id);
        //判断是否状态为审核中与审核未通过
        if (appInfo.getStatus().equals(SUCCESS_1)  ||appInfo.getStatus().equals(SUCCESS_3)){
            //把原本的数据放入到model
            model.addAttribute("info",appInfo);
            return "updateapp";
        }

        return "redirect:/app/list";

    }
    @PostMapping("/update")
    public String update(AppInfo appInfo,HttpSession session){
        //当前用户
        DevUser u= (DevUser) session.getAttribute("user");
        appInfo.setModifyby(u.getId());
        if (appInfoService.update(appInfo)){

        }
        return "redirect:/app/list";
    }

}
